<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>Express搭建后台服务 | 添柴阿杰</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="Express 是一款流行的、基于 Node.js 平台的 Web 应用程序开发框架，可以帮助开发人员快速创建 Web 服务器和 API。它提供了一组简单而灵活的工具，使得开发人员可以轻松地处理 HTTP 请求、响应以及中间件等操作。 这次我们先从实现Http请求响应入手 环境准备使用 npm 安装 Express 和其他相关包： 1npm install express body-parser">
<meta property="og:type" content="article">
<meta property="og:title" content="Express搭建后台服务">
<meta property="og:url" content="https://hajcker_1.gitee.io/hajcker_1.gitee.io/2023/04/14/Express%E6%90%AD%E5%BB%BA%E5%90%8E%E5%8F%B0%E6%9C%8D%E5%8A%A1/index.html">
<meta property="og:site_name" content="添柴阿杰">
<meta property="og:description" content="Express 是一款流行的、基于 Node.js 平台的 Web 应用程序开发框架，可以帮助开发人员快速创建 Web 服务器和 API。它提供了一组简单而灵活的工具，使得开发人员可以轻松地处理 HTTP 请求、响应以及中间件等操作。 这次我们先从实现Http请求响应入手 环境准备使用 npm 安装 Express 和其他相关包： 1npm install express body-parser">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-04-14T01:10:00.000Z">
<meta property="article:modified_time" content="2023-04-14T01:29:30.194Z">
<meta property="article:author" content="添柴阿杰">
<meta property="article:tag" content="Node">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/hajcker_1.gitee.io/atom.xml" title="添柴阿杰" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/hajcker_1.gitee.io/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/hajcker_1.gitee.io/css/style.css">

  
    
<link rel="stylesheet" href="/hajcker_1.gitee.io/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 6.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/hajcker_1.gitee.io/" id="logo">添柴阿杰</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/hajcker_1.gitee.io/">Home</a>
        
          <a class="main-nav-link" href="/hajcker_1.gitee.io/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/hajcker_1.gitee.io/atom.xml" title="RSS 订阅"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="搜索"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="搜索"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://hajcker_1.gitee.io/hajcker_1.gitee.io"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-Express搭建后台服务" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/hajcker_1.gitee.io/2023/04/14/Express%E6%90%AD%E5%BB%BA%E5%90%8E%E5%8F%B0%E6%9C%8D%E5%8A%A1/" class="article-date">
  <time class="dt-published" datetime="2023-04-14T01:10:00.000Z" itemprop="datePublished">2023-04-14</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      Express搭建后台服务
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <p>Express 是一款流行的、基于 Node.js 平台的 Web 应用程序开发框架，可以帮助开发人员快速创建 Web 服务器和 API。它提供了一组简单而灵活的工具，使得开发人员可以轻松地处理 HTTP 请求、响应以及中间件等操作。</p>
<p>这次我们先从实现Http请求响应入手</p>
<h3 id="环境准备"><a href="#环境准备" class="headerlink" title="环境准备"></a>环境准备</h3><p>使用 npm 安装 Express 和其他相关包：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install express body-parser cors</span><br></pre></td></tr></table></figure>

<h3 id="创建项目"><a href="#创建项目" class="headerlink" title="创建项目"></a>创建项目</h3><p>在空文件夹下通过vscode打开并在终端输入：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm init</span><br></pre></td></tr></table></figure>
<p>初始化一下项目。<br>紧接着可以创建一个名为app.js的文件：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> express = <span class="built_in">require</span>(<span class="string">&#x27;express&#x27;</span>);</span><br><span class="line"><span class="keyword">const</span> bodyParser = <span class="built_in">require</span>(<span class="string">&#x27;body-parser&#x27;</span>);</span><br><span class="line"><span class="keyword">const</span> cors = <span class="built_in">require</span>(<span class="string">&#x27;cors&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> app = <span class="title function_">express</span>();</span><br><span class="line"><span class="keyword">const</span> router = express.<span class="title class_">Router</span>();</span><br><span class="line"><span class="comment">// 路由部分</span></span><br><span class="line">router.<span class="title function_">get</span>(<span class="string">&#x27;/&#x27;</span>, <span class="function">(<span class="params">req, res</span>) =&gt;</span> &#123;</span><br><span class="line">  res.<span class="title function_">send</span>(<span class="string">&#x27;Hello World!&#x27;</span>);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">router.<span class="title function_">get</span>(<span class="string">&#x27;/user/:id&#x27;</span>, <span class="function">(<span class="params">req, res</span>) =&gt;</span> &#123;</span><br><span class="line">  res.<span class="title function_">send</span>(<span class="string">`User ID: <span class="subst">$&#123;req.params.id&#125;</span>`</span>);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">router.<span class="title function_">post</span>(<span class="string">&#x27;/user&#x27;</span>, <span class="function">(<span class="params">req, res</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">const</span> &#123; name, email &#125; = req.<span class="property">body</span>;</span><br><span class="line">  res.<span class="title function_">send</span>(<span class="string">`User Name: <span class="subst">$&#123;name&#125;</span> \n User Email: <span class="subst">$&#123;email&#125;</span>`</span>);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">app.<span class="title function_">use</span>(<span class="string">&#x27;/&#x27;</span>, router);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 中间件</span></span><br><span class="line">app.<span class="title function_">use</span>(bodyParser.<span class="title function_">json</span>());</span><br><span class="line">app.<span class="title function_">use</span>(<span class="title function_">cors</span>());</span><br><span class="line"></span><br><span class="line"><span class="comment">// 配置端口</span></span><br><span class="line"><span class="keyword">const</span> port = process.<span class="property">env</span>.<span class="property">PORT</span> || <span class="number">3000</span>;</span><br><span class="line"></span><br><span class="line">app.<span class="title function_">listen</span>(port, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">`Server is running on port <span class="subst">$&#123;port&#125;</span>.`</span>);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>保存后在终端输入：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">node app.js</span><br></pre></td></tr></table></figure>
<p>即可启动服务</p>
<h3 id="怎么理解中间件"><a href="#怎么理解中间件" class="headerlink" title="怎么理解中间件"></a>怎么理解中间件</h3><p>通过Node或者基于Node框架搭建的各种服务中，通常都会需要引入中间件来丰富功能性，从而提高复用性、可维护性和拓展性。那么中间件该怎么理解呢？</p>
<p>作为前端开发，我们在前端项目中经常会引入lodash.js、echarts等三方组件库，同理node程序中的中间件也是格外的。</p>
<p>举个简单的例子：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">myLogger</span>(<span class="params">req, res, next</span>) &#123;</span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;Logging...&#x27;</span>);</span><br><span class="line">  <span class="title function_">next</span>();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">app.<span class="title function_">use</span>(myLogger);</span><br></pre></td></tr></table></figure>
<p>我们定义一个打印日志的函数，绑定到上述app.js文件中创建的服务实例上，那么服务在每次处理请求时，就都会经过这样一层处理，打印出一句<strong>Logging</strong></p>
<h3 id="怎么理解路由"><a href="#怎么理解路由" class="headerlink" title="怎么理解路由"></a>怎么理解路由</h3><p>是指根据客户端请求的 URL 地址和 HTTP 请求方法，将请求转发到相应的处理程序或控制器的过程。即结合我们想实现的接口方法、接口类型（根据业务，我们可以自由定义）、接口规范，我们配置出<br>前端在请求服务时，需要访问服务的路径。<br>在上述服务中，启动后访问<a href="http://localhost:3000，">http://localhost:3000，</a> 即可在页面中看到<strong>Hello World</strong>的字样。</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="https://hajcker_1.gitee.io/hajcker_1.gitee.io/2023/04/14/Express%E6%90%AD%E5%BB%BA%E5%90%8E%E5%8F%B0%E6%9C%8D%E5%8A%A1/" data-id="clh8tq38q00044gvg4ala9dib" data-title="Express搭建后台服务" class="article-share-link">分享</a>
      
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/hajcker_1.gitee.io/tags/Node/" rel="tag">Node</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
  
    <a href="/hajcker_1.gitee.io/2023/04/09/%E8%B7%9F%E7%9D%80Chat%E5%AD%A6%E4%B9%A0React%E4%B9%8BHooks/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">后一篇</strong>
      <div class="article-nav-title">跟着Chat学习React之学习Hooks</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">标签</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/hajcker_1.gitee.io/tags/Node/" rel="tag">Node</a></li><li class="tag-list-item"><a class="tag-list-link" href="/hajcker_1.gitee.io/tags/React/" rel="tag">React</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">标签云</h3>
    <div class="widget tagcloud">
      <a href="/hajcker_1.gitee.io/tags/Node/" style="font-size: 10px;">Node</a> <a href="/hajcker_1.gitee.io/tags/React/" style="font-size: 20px;">React</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/hajcker_1.gitee.io/archives/2023/04/">四月 2023</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/hajcker_1.gitee.io/2023/04/14/Express%E6%90%AD%E5%BB%BA%E5%90%8E%E5%8F%B0%E6%9C%8D%E5%8A%A1/">Express搭建后台服务</a>
          </li>
        
          <li>
            <a href="/hajcker_1.gitee.io/2023/04/09/%E8%B7%9F%E7%9D%80Chat%E5%AD%A6%E4%B9%A0React%E4%B9%8BHooks/">跟着Chat学习React之学习Hooks</a>
          </li>
        
          <li>
            <a href="/hajcker_1.gitee.io/2023/04/07/%E8%B7%9F%E7%9D%80Chat%E5%AD%A6%E4%B9%A0React%E4%B9%8B%E5%88%9B%E5%BB%BA%E5%88%9D%E5%A7%8B%E9%A1%B9%E7%9B%AE/">跟着Chat学习React之创建初始项目</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2023 添柴阿杰<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/hajcker_1.gitee.io/" class="mobile-nav-link">Home</a>
  
    <a href="/hajcker_1.gitee.io/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/hajcker_1.gitee.io/js/jquery-3.4.1.min.js"></script>



  
<script src="/hajcker_1.gitee.io/fancybox/jquery.fancybox.min.js"></script>




<script src="/hajcker_1.gitee.io/js/script.js"></script>





  </div>
</body>
</html>